package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.listener.SuixingfuExcelListener;
import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.modular.system.entity.BtySuixingfu;
import cn.stylefeng.guns.sys.modular.system.entity.Yybb;
import cn.stylefeng.guns.sys.modular.system.mapper.BtySuixingfuMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtySuixingfuParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtySuixingfuResult;
import cn.stylefeng.guns.sys.modular.system.service.BtySuixingfuService;
import cn.stylefeng.guns.sys.modular.system.service.YybbService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;


/**
 * 控制器
 *
 * @author liuy
 * @Date 2022-04-27 10:14:39
 */
@Controller
@RequestMapping("/btySuixingfu")
public class BtySuixingfuController extends BaseController {

    private String PREFIX = "/modular/system/btySuixingfu";

    @Autowired
    private BtySuixingfuService btySuixingfuService;
    @Resource
    private BtySuixingfuMapper suixingfuMapper;
    @Resource
    private YybbService yybbService;


    /**
     * 跳转到主页面
     *
     * @author liuy
     * @Date 2022-04-27
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/btySuixingfu.html";
    }

    /**
     * 新增页面
     *
     * @author liuy
     * @Date 2022-04-27
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/btySuixingfu_add.html";
    }

    /**
     * 编辑页面
     *
     * @author liuy
     * @Date 2022-04-27
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/btySuixingfu_edit.html";
    }

    /**
     * 新增接口
     *
     * @author liuy
     * @Date 2022-04-27
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BtySuixingfuParam btySuixingfuParam) {
        this.btySuixingfuService.add(btySuixingfuParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author liuy
     * @Date 2022-04-27
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BtySuixingfuParam btySuixingfuParam) {
        this.btySuixingfuService.update(btySuixingfuParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author liuy
     * @Date 2022-04-27
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BtySuixingfuParam btySuixingfuParam) {
        this.btySuixingfuService.delete(btySuixingfuParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author liuy
     * @Date 2022-04-27
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BtySuixingfuParam btySuixingfuParam) {
        BtySuixingfu detail = this.btySuixingfuService.getById(btySuixingfuParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author liuy
     * @Date 2022-04-27
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BtySuixingfuParam btySuixingfuParam) {
        return this.btySuixingfuService.findPageBySpec(btySuixingfuParam);
    }

    /**
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public Object addUsers(@RequestParam("file") MultipartFile file) throws IOException {
        JsonResult jsonResult = new JsonResult();
        //导入之前删掉之前的数据
        suixingfuMapper.delete(new QueryWrapper<>());
        try {
            SuixingfuExcelListener basicExcelListener = new SuixingfuExcelListener(btySuixingfuService);
            EasyExcel.read(file.getInputStream(), BtySuixingfu.class, basicExcelListener).sheet(0).doRead();
//            //导入成功之后获取微信到账金额和手续费更新到yybb中
            List<BtySuixingfuResult> list = btySuixingfuService.sxfList();
            for (BtySuixingfuResult sxfResult : list) {
                String dm = sxfResult.getDm();
                String yyr = sxfResult.getYyr();
                BigDecimal dzje = sxfResult.getDzje();
                BigDecimal sxf = sxfResult.getSxf();
                //根据店名和营业日更新微信金额到yybb中
                Yybb yybb = yybbService.getOne(new QueryWrapper<Yybb>().eq("dianming", dm).eq("yyr", yyr));
                if (ToolUtil.isNotEmpty(yybb)) {
                    //差异=随行付实收+手续费-随行付应收
                    BigDecimal sxfys=yybb.getSxfys();
                    BigDecimal sxfcy=BigDecimal.ZERO;
                    yybb.setSxfsj(dzje);
                    yybb.setSxfsxf(sxf);
                    sxfcy=dzje.add(sxf).subtract(sxfys);
                    yybb.setSxfcy(sxfcy);
                    boolean flag = yybbService.updateById(yybb);
                    if (flag == true) {
                        BtySuixingfuParam param = new BtySuixingfuParam();
                        param.setDm(dm);
                        param.setYyr(yyr);
                        btySuixingfuService.updateStateByDm(param);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        return true;
    }

}


